package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IHomepageVORedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
public class HomepageVORedisServiceImpl implements IHomepageVORedisService {

    @Autowired
    private StringRedisTemplate template;
    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public int todayViewnumIncrease(String uid) {

        int viewNum = this.getTodayViewNum(uid);

        // 统一 +1   绝了
        viewNum++;

        // 更新vo
        this.setTodayViewNum(viewNum, uid);

        return viewNum;
    }

    @Override
    public int getTodayViewNum(String uid) {

        // 拼接key
        // 今日
        String key = RedisKeys.HOMEPAGE_TODAY.join(uid);

        // 判断key是否存在
        int viewnum = 0;
        if (!template.hasKey(key)) {
            // 不存在先创建

            // 有效时间: 今天最后一秒 - 当前时间
            Date now = new Date();
            Date end = DateUtil.getEndDate(now);
            Long time = DateUtil.getDateBetween(now, end);
            template.opsForValue().set(key, JSON.toJSONString(viewnum), time, TimeUnit.SECONDS);

            // 更新vo
            this.setTodayViewNum(viewnum, uid);
        } else {
            // 存在就取出来
            String voStr = template.opsForValue().get(key);
            if (StringUtils.hasText(voStr)) {
                viewnum = new Integer(voStr);
            } else {
                viewnum = -1;
            }
        }

        return viewnum;
    }

    @Override
    public void setTodayViewNum(int viewNum, String uid) {
        // 拼接key
        String key = RedisKeys.HOMEPAGE_TODAY.join(uid);
        // 更新vo
        // 有效时间: 今天最后一秒 - 当前时间
        Date now = new Date();
        Date end = DateUtil.getEndDate(now);
        Long time = DateUtil.getDateBetween(now, end);
        template.opsForValue().set(key, JSON.toJSONString(viewNum), time, TimeUnit.SECONDS);

    }

    @Override
    public int totalViewnumIncrease(String uid) {
        int viewNum = this.getTotalViewNum(uid);

        // 统一 +1   绝了
        viewNum++;

        // 更新vo
        this.setTotalViewNum(viewNum, uid);

        return viewNum;
    }

    @Override
    public int getTotalViewNum(String uid) {
        // 拼接key
        // 总
        String key = RedisKeys.HOMEPAGE_TOTAL.join(uid);

        // 判断key是否存在
        int viewnum = 0;
        if (!template.hasKey(key)) {
            // 不存在先创建

            // 有效时间: 永久

            // 更新vo
            this.setTodayViewNum(viewnum, uid);
        } else {
            // 存在就取出来
            String voStr = template.opsForValue().get(key);
            if (StringUtils.hasText(voStr)) {
                viewnum = new Integer(voStr);
            } else {
                viewnum = -1;
            }
        }

        return viewnum;
    }

    @Override
    public void setTotalViewNum(int viewNum, String uid) {
        // 拼接key
        String key = RedisKeys.HOMEPAGE_TOTAL.join(uid);
        // 更新vo
        template.opsForValue().set(key, JSON.toJSONString(viewNum));
    }
}
